package edu.jd.log;

import edu.jd.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class LogService {

    public void writeLog(Log log){
        SqlSession sess = Utils.openSession();
        try{
            LogDao dao = sess.getMapper(LogDao.class);
            dao.insertLog(log);
            sess.commit();
        }catch (Exception e){
            e.printStackTrace();
            sess.rollback();
            throw new RuntimeException("记录日志失败");
        }finally {
            sess.close();
        }
    }

    public List<LogDto> getLogs(){
        SqlSession sess = Utils.openSession();
        try{
            LogDao dao = sess.getMapper(LogDao.class);
            List<LogDto> list = dao.getAllLog();
            sess.commit();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            sess.rollback();
            throw new RuntimeException(" 获取日志失败");
        }finally {
            sess.close();
        }
    }
}
